package com.shujia.mapreduce;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;

import java.io.IOException;

public class Demo1StudentScore {


    /**
     * map端
     * 读取学生分数表
     * <p>
     * 以学号作为key  分数作为value输出
     */

    static class StudentScoreMap extends Mapper<LongWritable, Text, Text, LongWritable> {
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

            String line = value.toString();

            String[] split = line.split(",");

            String s_id = split[0];
            Integer score = Integer.parseInt(split[2]);
            //以学号作为key  分数作为value输出
            context.write(new Text(s_id), new LongWritable(score));
        }
    }

    //同一个学生的数据在同一个reduce中进行处理
    static class StudentScoreReduce extends Reducer<Text, LongWritable, NullWritable, Text> {

        /**
         * Iterable  迭代器
         * <p>
         * 数据并没有全部加载到内存   没迭代一次读取一次
         * <p>
         * 迭代一次之后数据就没有了
         */
        @Override
        protected void reduce(Text key, Iterable<LongWritable> values, Context context) throws IOException, InterruptedException {

            long sum = 0L;

            //计算学生总分
            for (LongWritable value : values) {
                sum = sum + value.get();
            }

            /**
             * 默认输出  kv以\t分割   只起到占位的作用
             *
             * NullWritable  相当于没有
             *
             */

            String line = key + "," + sum;


            context.write(NullWritable.get(), new Text(line));

        }
    }

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {

        /**
         *
         * 1、统计学生的总分
         * 	需要对一个学生的分数进行处理
         * 	需要将同一个学生的所有分数拉取到同一个reduce中进行处理
         * 	map 端输出 以学号作为key  以分数作为value
         *
         */

        Configuration config = new Configuration();

        /**
         * 构建mapreduce 任务
         *
         */
        //创建job任务对象
        Job job = Job.getInstance(config);

        job.setJobName("Demo1StudentScore");

        job.setJarByClass(Demo1StudentScore.class);

        //设置map端 输出类型
        job.setMapperClass(StudentScoreMap.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(LongWritable.class);



        //设置reduce端
        job.setReducerClass(StudentScoreReduce.class);

        //设置mapreduc任务输出数类型
        job.setOutputKeyClass(NullWritable.class);
        job.setOutputValueClass(Text.class);

        Path path = new Path("/data/score");


        //值输入输出路径
        TextInputFormat.addInputPath(job, path);

        Path outPath = new Path("/data/sum_score");
        TextOutputFormat.setOutputPath(job, outPath);


        job.waitForCompletion(true);


    }
}
